<template>
  <!--              <mavon-editor placeholder="开始编辑...." :subfield="false" style="height: 500px" ref=md-->
  <!--                v-model="form.aboutMe" />-->
  <div style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :default-config="toolbarConfig"
      :mode="mode"
    />
    <Editor
      ref="editor"
      v-model="updateVal"
      style="height: 500px; overflow-y: hidden;"
      :default-config="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
      @onChange="updateData"
    />
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
  name: 'RichText',
  components: { Editor, Toolbar },
  props: {
    aboutMe: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      updateVal: '',
      editor: null,
      toolbarConfig: {},
      editorConfig: {
        placeholder: '请输入内容...',
        // height: 500,
        MENU_CONF: {
          uploadImage: {
            server: process.env.VUE_APP_BASE_API + '/file/uploadFile',
            fieldName: 'file',
            headers: {},
            customInsert: (res, insertFn) => {
              insertFn(res.data[0].fileUrl)
            }
          }
        }
      },
      mode: 'default' // or 'simple'
    }
  },
  watch: {
    aboutMe(newValue) {
      this.updateVal = newValue
    }
  },
  created() {
    this.updateVal = this.aboutMe
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    updateData() {
      this.$emit('childUpdated', this.updateVal)
    }
  }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
